package com.msmall.community.play.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.exception.DataNotFoundException;
import com.msmall.base.pager.MatchType;
import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.play.dao.PlayDao;
import com.msmall.community.play.model.Play;
import com.msmall.community.play.model.UserPlay;
import com.msmall.community.play.service.PlayManager;
import com.msmall.community.user.service.UserManager;

public class PlayManagerImpl extends BaseManagerImpl<Play, Long> implements PlayManager{

	private static final Log logger = LogFactory.getLog(PlayManagerImpl.class);
	
	private PlayDao playDao;

	private UserManager userManager;
	
	public void setPlayDao(PlayDao playDao) {
		this.playDao = playDao;
	}
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void savePlay(Play play, Long creator) {
		if(null == play.getEnabled())
			play.setEnabled(ConstantDB.UNAVALIABLE);
		play.setCreator(creator);
		play.setCreateTime(new Date());
		playDao.save(play);
	}

	public List<Play> getByUserid(Long userid) throws Exception {
		return playDao.getByUserid(userid);
	}

	/**
	 * 先从缓存获得，如果为NULL，查询数据库
	 */
	public Play getPlay(Long playId)throws Exception{
		try{
			List list = (List)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_PLAY_LIST);
			if(list == null || list.size() <= 0){
				return loadPlay(playId);
			}else{
				Play play = null;
				for(int i = 0; i < list.size(); i ++){
					play = (Play) list.get(i);
					if(playId.compareTo(play.getPlayId()) == 0){
						break;
					}else{
						play = null;
					}
				}
				if(play == null)
					throw new DataNotFoundException();
				return play;
			}
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 查询数据库
	 * @param playId
	 * @return
	 */
	public Play loadPlay(Long playId)throws Exception{
		try{
			List list  = playDao.getByProperty(Play.class, "playId", MatchType.EQ, String.valueOf(playId));
			if(list != null && list.size() > 0)
				return (Play) list.get(0);
			else{
				throw new DataNotFoundException();
			}
		}catch(Exception e){
			logger.error(e);
			throw e;
		}
	}
	/**
	 * 先从缓存获得，如果为NULL，重新查询数据库
	 */
	public List<Play> getAllPlays() throws Exception{
		List list = (List)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_PLAY_LIST);
		if(list == null || list.size() <= 0){
			return getAll(Play.class);
		}else{
			return list;
		}
	}
	
	/**
	 * 获得所有有效的PLAY
	 * @param page
	 * @throws Exception
	 */
	public void getAvaliablePlays(PaginationSupport page) throws Exception {
		page.setClazz(Play.class);
		page.addFilter("enabled", MatchType.EQ, ConstantDB.AVALIABLE);
		playDao.getPagination(page);
	}
	
	/**
	 * 用户加入PLAY,首先检查是否已经加入了，如果已经加入返回FALSE
	 * @param userid
	 * @param playId
	 * @return
	 * @throws Exception
	 */
	public boolean joinPlay(Long userid, Long playId) throws Exception{
		try{
			UserPlay upa = playDao.getUserPlay(userid, playId);
			if(upa != null)
				return false;
			Play play = loadPlay(playId);
			UserPlay up = new UserPlay();
			up.setPlay(play);
			up.setUserid(userid);
			up.setEnabled(ConstantDB.AVALIABLE);
			up.setCreateTime(new Date());
			playDao.saveObject(up);
			return true;
		}catch(Exception e){
			logger.error("Join play error!");
			throw e;
		}
	}
	
	/**
	 * 用户删除已经加入的Play
	 * @param userid
	 * @param playId
	 * @return
	 * @throws Exception
	 */
	public boolean leavePlay(Long userid, Long playId) throws Exception{
		try{
			UserPlay up = playDao.getUserPlay(userid, playId);
			up.setEnabled(ConstantDB.UNAVALIABLE);
			playDao.saveObject(up);
			return true;
		}catch(Exception e){
			logger.error("Leave play error!");
			throw e;
		}
	}
}
